<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
	<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
	<title></title>
	<script type="text/javascript" src="../../js/boot.js"></script>
</head>

<body>
	<div style="width:99%;height:99%;">
		<div style="float:left; width:35%; height:100%;">
			<fieldset style="width: 100%;height:100%;">
				<legend><span>工时分项</span></legend>
				<div class="mini-fit">
					<ul id="sortTree" class="mini-tree" style="width:258px;" showTreeIcon="true" allowDrag="true" allowDrop="true" textField="sortName"
						idField="projectSortID" resultAsTree="false">
					</ul>
				</div>
			</fieldset>
		</div>
		<div style="float:right; width:65%; height:100%;">
			<fieldset style="width: 100%;height:240px;">
				<legend><span>日报内容</span></legend>
				<div class="mini-fit">
					<div id="dayForm" style="padding:10px;">
						<table align="center">
							<tr>
								<td align="right">用户姓名：</td>
								<td align="left"><input id="userName" name="userName" class="mini-textbox" enabled="false"></td>
								<td align="right">部门名称：</td>
								<td align="left"><input id="departName" name="departName" class="mini-textbox" enabled="false"></td>
							</tr>
							<tr>
								<td align="right">日期：</td>
								<td align="left"><input id="calendarDate" name="calendarDate" class="mini-textbox" enabled="false"></td>
								<td align="right">工作时长：</td>
								<td align="left"><input id="workTime" name="workTime" class="mini-textbox" enabled="false"></td>
							</tr>
							<tr>
								<td align="right">加班时长：</td>
								<td align="left"><input id="overTime" name="overTime" class="mini-textbox" enabled="false"></td>
								<td align="right">请假时长：</td>
								<td align="left"><input id="applyTime" name="applyTime" class="mini-textbox" enabled="false"></td>
							</tr>
							<tr>
								<td align="right">刷卡记录：</td>
								<td align="left" colspan="3"><input name="cardRecord" class="mini-textbox" width="400" enabled="false"></td>
							</tr>
							<tr>
								<td align="right">提交时间：</td>
								<td align="left"><input name="submitTime" class="mini-datepicker" format="yyyy-MM-dd HH:mm" enabled="false"></td>
								<td align="right">审核时间：</td>
								<td align="left"><input name="checkedTime" class="mini-datepicker" format="yyyy-MM-dd HH:mm" enabled="false"></td>
							</tr>
							<tr>
								<td align="right">审核备注：</td>
								<td align="left" colspan="3"><input id="checkRemark" name="checkRemark" class="mini-textbox" width="400"></td>
							</tr>
						</table>
					</div>
				</div>
			</fieldset>

			<fieldset style="width: 100%;height:400px;">
				<legend><span>工作内容</span></legend>
				<div class="mini-fit">
					<div id="detailForm" style="padding:10px;">
						<input id="projectDayID" name="projectDayID" class="mini-hidden">
						<table align="center">
							<tr>
								<td align="right">项目名称：</td>
								<td align="left">
									<div id="projectID" name="projectID" class="mini-combobox" style="width:150px;" popupWidth="300" textField="projectName" valueField="projectID"
										showClose="true" oncloseclick="onCloseClick">
										<div property="columns">
											<div header="项目编号" field="projectNo"></div>
											<div header="项目名称" field="projectName"></div>
										</div>
									</div>
								</td>
								<td align="right">工作时长：</td>
								<td align="left"><input id="workTime" name="workTime" class="mini-spinner" increment="0.5" minValue="0" maxValue="12"></td>
							</tr>
							<tr>
								<td align="right">工作包主名称：</td>
								<td align="left"><input id="projectMainPacketID" class="mini-combobox" textField="packetName" valueField="projectPacketID"></td>
								<td align="right">工作包子名称：</td>
								<td align="left"><input id="projectPacketID" name="projectPacketID" class="mini-combobox" textField="packetName"
										valueField="projectPacketID"></td>
							</tr>
							<tr>
								<td align="right">出勤分类：</td>
								<td align="left" colspan="3">
									<div id="workSort" name="workSort" class="mini-radiobuttonlist" repeatLayout="table" textField="text" valueField="id"
										data="workSortData" width="360">
									</div>
								</td>
							</tr>
							<tr>
								<td align="right">图纸名称：</td>
								<td align="left" colspan="3">
									<input id="projectDrawID" name="projectDrawID" class="mini-autocomplete" textField="drawName" valueField="projectDrawID"
										width="320">
									<a class="mini-button" iconCls="icon-zoomin" onClick="openDrawWindow()">参照</a>
								</td>
							</tr>
							<tr>
								<td align="right">工作说明：</td>
								<td align="left" colspan="3"><textarea id="description" name="description" class="mini-textarea" width="400"></textarea></td>
							</tr>
							<tr>
								<td align="right">完成进度：</td>
								<td align="left"><input id="hasWorkedRate" name="hasWorkedRate" class="mini-combobox" textField="text" valueField="id"
										data="workedRate"></td>
								<td align="right"></td>
								<td align="left"></td>
							</tr>
						</table>
					</div>
					<div property="footer" style="text-align:center;padding:5px;padding-right:15px;">
						<a class="mini-button" iconCls="icon-save" id="doSave" onClick="doSave('close')">保存</a>
						<a class="mini-button" iconCls="icon-save" id="doSaveInsert" onClick="doSave('continue')">保存并新增</a>
					</div>
				</div>
			</fieldset>
		</div>
	</div>

</body>

<script type="text/javascript">
	var localUser = null;
	var userID = null;
	var obj = localStorage.getItem('user');
	if (obj) {
		localUser = mini.decode(obj);
		userID = localUser.userID;
	}

	// 工作进度
	var workedRate = [
		{ id: 0, text: "0%" },
		{ id: 0.1, text: "10%" },
		{ id: 0.2, text: "20%" },
		{ id: 0.3, text: "30%" },
		{ id: 0.4, text: "40%" },
		{ id: 0.5, text: "50%" },
		{ id: 0.6, text: "60%" },
		{ id: 0.7, text: "70%" },
		{ id: 0.8, text: "80%" },
		{ id: 0.9, text: "90%" },
		{ id: 1, text: "100%" }
	];

	var workSortData = [
		{ id: 1, text: "正常出勤" },
		{ id: 2, text: "正常加班" },
		{ id: 3, text: "周末加班" },
		{ id: 4, text: "假日加班" }
	];

	miniInit();

	mini.get("hasWorkedRate").select(0);

	// 工时分项
	var sortTree = mini.get("sortTree");
	var projectSortID_key = null; // 项目工时的主键
	var applyTimeSortID_Key = ""; // 非出勤工时主键
	http.get("/oa/project/sort/list.do", function (page) {
		if (page.data.length > 0) {
			sortTree.loadList(page.data, "projectSortID", "parentID");
			var root = sortTree.getRootNode();
			var children = sortTree.getChildNodes(root);
			children = sortTree.getChildNodes(children[0]);
			// debugger
			for (var i = 0; i < children.length; i++) {
				if (children[i].sortName == "项目工时") {
					projectSortID_key = children[i].projectSortID;
				} else if (children[i].sortName == "非出勤工时") {
					applyTimeSortID_Key = children[i].projectSortID;
				}
			}
		} else {
			sortTree.addNode({
				projectSortID: "ROOT",
				sortName: "公司"
			}, 0, null);
		}
	});

	// 项目名称控件数据
	var projectCombox = mini.get("projectID");
	var projectUrl = "/oa/project/project/list.do?status=1&orderBy=projectNo";
	if (userID != null) {
		projectUrl = projectUrl + "&userID=" + userID;
	}
	http.get(projectUrl, function (page) {
		if (page.data.length > 0) {
			projectCombox.setData(page.data);
		}
	});

	// 图纸控件数据
	var projectDrawIDComBox = mini.get("projectDrawID");
	var projectDrawIDUrl = "/project/draw/page.do";
	if (userID != null) {
		projectDrawIDUrl = projectDrawIDUrl + "?userID=" + userID;
	}
	projectDrawIDComBox.set({
		url: http.getUrl(projectDrawIDUrl)
	});

	// 工作包主名称
	var projectMainPacketIDComBox = mini.get("projectMainPacketID");
	projectMainPacketIDComBox.set({
		url: http.getUrl("/oa/project/packet/list.do?parentID=ROOT")
	});
	// 工作包子名称
	var projectPacketIDComBox = mini.get("projectPacketID");
	var projectPacketIDUrl = "/oa/project/packet/list.do?parentID=";
	if (projectPacketIDComBox.getValue() != "") {
		http.get(projectPacketIDUrl + projectMainPacketIDComBox.getValue(), function (page) {
			if (page.data.length > 0) {
				projectPacketIDComBox.setData(page.data);
			}
		});
	}
	// 主目录切换，子目录同步变更
	projectMainPacketIDComBox.on("valuechanged", function (e) {
		projectMainPacketIDComBoxChange(e.value);
	});

	function projectMainPacketIDComBoxChange(value) {
		http.get(projectPacketIDUrl + value, function (page) {
			if (page.data.length > 0) {
				projectPacketIDComBox.setData(page.data);
			}
		});
	}

	// 从后台获取用户个人配置信息
	var userConfig = {};
	http.get("/user/get.do?userID=" + localUser.userID, function (page) {
		var data = page.data.config;
		if (data && data.length > 0) {
			userConfig = JSON.parse(data);
			if (userConfig.projectMainPacketID && userConfig.projectMainPacketID != "") {
				projectMainPacketIDComBoxChange(userConfig.projectMainPacketID);
			}
		}
	});

	function cancel() { }

	var dayWorkData = {}; // 分时分项工作数据，是Map类数据，主键是projectSortID工时分项编号
	var dayReportID_ = null;
	var editNode = null;
	var dayForm = new mini.Form("#dayForm");
	var detailForm = new mini.Form("#detailForm");

	function setData(dayReportID, projectDayID) {
		dayReportID_ = dayReportID;
		var dayReport = null;
		http.getSync("/day/report/getDayReport.do?dayReportID=" + dayReportID, function (page) {
			if (page.success) {
				dayReport = page.data;
				dayForm.setData(dayReport);
				// 如果不是本用户的数据，则保存和提交按钮不可用
				if (localUser.userID != dayReport.userID) {
					mini.get("doSave").setEnabled(false);
					mini.get("doSaveInsert").setEnabled(false);
				}
				// 如果提交状态不是未提交，则保存和提交按钮不可用
				if (dayReport.checkStatus > 0) {
					mini.get("doSave").setEnabled(false);
					mini.get("doSaveInsert").setEnabled(false);
				}
			}
		});
		if (projectDayID) {
			http.get("/day/report/getDayReportDetail.do?projectDayID=" + projectDayID, function (page) {
				if (page.success) {
					// debugger
					var bean = page.data;
					detailForm.setData(bean);
					var node = sortTree.getNode(bean.projectSortID);
					sortTree.expandPath(node); // 展开终端结点用expandPath方法
					sortTree.checkNode(node);
					if (bean.projectPacketID && bean.projectPacketID != "") {
						var str = bean.projectPacketID;
						str = str.substr(0, str.length - 4);
						bean.projectMainPacketID = str;
					}
					if (editNode == null) {
						editNode = node;
					}
					sortTree.selectNode(editNode);
					projectMainPacketIDComBox.setValue(bean.projectMainPacketID);
					projectMainPacketIDComBoxChange(bean.projectMainPacketID);
					projectPacketIDComBox.setValue(bean.projectPacketID);
					if (bean.projectDrawID && bean.projectDrawID != "") {
						// debugger
						projectDrawIDComBox.setText(bean.drawName);
						projectDrawIDComBox.setValue(bean.projectDrawID);
					} else {
						projectDrawIDComBox.setText("");
						projectDrawIDComBox.setValue("");
					}
				}
			});
		} else {
			// 如果工作没有数据，那么就是新增。加载默认设置
			var sort = userConfig.sort;
			if (sort != null && sort.length > 0) {
				for (var i = 0; i < sort.length; i++) {
					// 把已编辑的节点展开
					var node = sortTree.getNode(sort[i]);
					sortTree.expandNode(node); // 展开结点下的所有子结点用expandNode方法
				}
			}
			setUserConfigInfo();
			// 工作日，假日，节日
			var workSort = 0;
			debugger
			if (dayReport.dayType == null || dayReport.dayType == "假日") {
				// 如果工作超过8小时，自动选择加班
				if (dayReport.totalWorkTime >= 8) {
					workSort = 2;
				} else {
					workSort = 1;
				}
			} else if (dayReport.dayType == "假日") {
				workSort = 3;
			} else if (dayReport.dayType == "节日") {
				workSort = 4;
			}
			mini.get("workSort").setValue(workSort);
		}
	}

	function setUserConfigInfo() {
		// 项目名称
		if (userConfig.projectID) {
			projectCombox.setValue(userConfig.projectID);
		}
		// 工作包主名称
		if (userConfig.projectMainPacketID) {
			projectMainPacketIDComBox.setValue(userConfig.projectMainPacketID);
			projectMainPacketIDComBoxChange(userConfig.projectMainPacketID);
		}
		// 工作包子名称
		if (userConfig.projectPacketID) {
			projectPacketIDComBox.setValue(userConfig.projectPacketID);
		}
	}

	sortTree.on("nodeclick", function (e) {
		editNode = e.node;
		// 如果是项目工时
		if (editNode.projectSortID.indexOf(projectSortID_key) != -1) {
			projectCombox.setEnabled(true);
			projectDrawIDComBox.setEnabled(true);
			projectMainPacketIDComBox.setEnabled(true);
			projectPacketIDComBox.setEnabled(true);
			setUserConfigInfo();
		} else {
			projectCombox.setEnabled(false);
			projectDrawIDComBox.setEnabled(false);
			projectMainPacketIDComBox.setEnabled(false);
			projectPacketIDComBox.setEnabled(false);
			projectCombox.setValue("");
			projectMainPacketIDComBox.setValue("");
			projectPacketIDComBox.setValue("");
		}
	});

	// 保存
	function doSave(type) {
		if (editNode == null) {
			miniui.showErrorMessageBox("请先选择工时分项！");
			return;
		}
		var data = detailForm.getData(true, false);
		if (data.workTime <= 0) {
			miniui.showErrorMessageBox("工作时长必须大于0！");
			return;
		}
		miniui.mask('数据保存中...');
		data.dayReportID = dayReportID_;
		data.projectSortID = editNode.projectSortID;
		data.userID = userID;
		data.calendarDate = mini.get("calendarDate").getValue();
		debugger
		// 如果是非出勤工时
		if (data.projectSortID.indexOf(applyTimeSortID_Key) != -1) {
			data.workType = 2;
		} else {
			data.workType = 1;
			// 如果是项目工时，项目名称不能为空
			if (data.projectSortID.indexOf(projectSortID_key) != -1) {
				var val = projectCombox.getValue();
				debugger
				if (val == "") {
					mini.unmask();
					miniui.showErrorMessageBox("项目工时必须要选择项目名称！");
					return;
				}
			}
		}
		http.post("/day/report/saveDayReportDetail.do", data, function (page) {
			mini.unmask();
			// debugger
			miniui.showPageMessageBox(page, "保存成功！", function () {
				if (type == "continue") {
					data.projectDayID = null;
					data.workTime = 0;
					data.description = "";
					detailForm.setData(data);
					mini.get("hasWorkedRate").select(0);
					projectDrawIDComBox.setText(null);
					projectDrawIDComBox.setValue(null);
				} else {
					closeWindow("ok");
				}
			});
		});
	}

	// 审批通过日报
	function doPass() {
		mini.mask({
			el: document.body,
			cls: 'mini-mask-loading',
			html: '数据保存中...'
		});
		var d = dayForm.getData(true, false);
		var data = {
			"dayReportID": d.dayReportID,
			"checkUserID": localUser.userID,
			"checkedTime": common.dateTimeToString(new Date()),
			"checkStatus": 2
		};
		http.post("/day/report/check.do", data, function (page) {
			mini.unmask();
			if (page.success) {
				miniui.showInfoTip("保存成功");
			} else {
				miniui.showInfoMessageBox(page.message);
			}
		});
	}

	// 审批不通过日报
	function doReject() {
		mini.mask({
			el: document.body,
			cls: 'mini-mask-loading',
			html: '数据保存中...'
		});
		var d = dayForm.getData(true, false);
		var data = {
			"dayReportID": d.dayReportID,
			"checkUserID": localUser.userID,
			"checkedTime": common.dateTimeToString(new Date()),
			"checkStatus": 0
		};
		http.post("/day/report/check.do", data, function (page) {
			mini.unmask();
			closeWindow("ok");
		});
	}

	function closeWindow(action) {
		if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
		else window.close();
	}

	// 打开个性化设置窗体
	function openDrawWindow() {
		mini.open({
			url: ctx + '/page/work/day_report_detail_draw_select.html',
			title: "编辑",
			width: 760,
			height: 480,
			onload: function () {
				//var iframe = this.getIFrameEl();
				//iframe.contentWindow.initial();
			},
			ondestroy: function (action) {
				if (action == "ok") {
					var iframe = this.getIFrameEl();
					var data = iframe.contentWindow.getData();
					// debugger
					projectDrawIDComBox.setText(data.drawName);
					projectDrawIDComBox.setValue(data.projectDrawID);
				}
			}
		});
	}
</script>

</html>